using System;
using System.Collections.Generic;

namespace Nyanimals
{
    public sealed class CEventManager
    {
        private List<CBaseEvent>     Events;
        private List<CEventListener> Clients;
        private static readonly CEventManager instance = new CEventManager();
        
        public static CEventManager Instance
        {
            get { return instance;}
        }
        
        private CEventManager()
        {
            Events = new List<CBaseEvent>();
            Clients = new List<CEventListener>();
        }
        
        public void RegisterClient(ref CEventListener listener)
        {
            Clients.Add(listener);
        }
        
        public void UnregisterClient(ref CEventListener listener)
        {
            Clients.Remove(listener);
        }
        
        public void QueueEvent(CBaseEvent e)
        {
            Console.WriteLine("Sent Event: " + e.ID.ToString());
            Events.Add(e); 
        }
        
        public void ProcessEvents()
        {
            // for each event
            foreach(CBaseEvent e in Events)
            {
                // send to each client
                foreach(CEventListener client in Clients)
                {
                    client.ProcessEvent(e);
                }
            }
            
            // clean events
            Events.Clear();
        }
        
    }
}

